package com.weaver.qfengx;

import weaver.general.Util;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.workflow.WorkflowComInfo;

import java.util.Map;
import java.util.Objects;

/**
 * 流程相关操作工具类
 * Created by YeShengtao on 2020/8/24 15:01
 */
public class WorkflowUtils {

    public static String requestIdToworkflowId(String requestId) {
        String sql = "select requestid, workflowid from workflow_requestbase where requestid = ?";
        return Objects.requireNonNull(DaoUtils.executeQueryToMap(sql, requestId)).get("workflowid");
    }

    public static String mainTable(RequestInfo requestInfo) {
        WorkflowComInfo workflowComInfo = new WorkflowComInfo();
        Util.getIntValue(workflowComInfo.getFormId("" + requestInfo.getWorkflowid()));
        return "formtable_main_" + -1 * Util.getIntValue(workflowComInfo.getFormId("" + requestInfo.getWorkflowid()));
    }

    public static String mainTable(String workflowId) {
        String sql = "select formid from workflow_base where id = ?";
        return "formtable_main_" + -1 * Util.getIntValue(Objects.requireNonNull(DaoUtils.executeQueryToMap(sql, workflowId)).get("formid"));
    }

    public static Map<String, String> data(String requestId, String workflowId) {
        String table = mainTable(workflowId);
        String sql = String.format("select * from %s where requestid = ?", mainTable(workflowId));
        return DaoUtils.executeQueryToMap(sql, requestId);
    }

    public static String creatorId(String requestId) {
        String sql = "select * from workflow_requestbase where requestid = ?";
        return Objects.requireNonNull(DaoUtils.executeQueryToMap(sql, requestId)).get("creater");
    }

    public static String prop(Property[] properties, String name) {
        for (Property property : properties) {
            if (StringUtils.equals(property.getName(), name)) {
                return Util.null2String(property.getValue());
            }
        }
        return "";
    }

    public static String currentNodeId(String requestId) {
        return DaoUtils.querySingleVal("select currentnodeid from workflow_requestbase where requestid = ?", requestId);
    }
}
